package com.starchen.optimalselection.web.controller;

import com.starchen.optimalselection.common.utils.Result;
import com.starchen.optimalselection.web.model.MemberAddress;
import com.starchen.optimalselection.web.service.MemberAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员收货地址管理Controller
 * Created by daxilong on 2018/8/28.
 */
@RestController
@Api(tags = "UmsMemberReceiveAddressController")
@RequestMapping("/api/address")
public class MemberAddressController {

    @Autowired
    private MemberAddressService memberReceiveAddressService;

    @ApiOperation("添加收货地址")
    @PostMapping(value = "/add")
    public Result add(@RequestBody MemberAddress address) {
        int count = memberReceiveAddressService.add(address);
        if (count > 0) {
            return Result.success(count);
        }
        return Result.error();
    }

    @ApiOperation("删除收货地址")
    @GetMapping(value = "/delete/{id}")
    public Result delete(@PathVariable Long id) {
        memberReceiveAddressService.delete(id);
        return Result.success();
    }

    @ApiOperation("修改收货地址")
    @PostMapping(value = "/update")
    public Result update(@RequestBody MemberAddress address) {
        memberReceiveAddressService.update(address);
        return Result.success();
    }

    @ApiOperation("获取所有收货地址")
    @GetMapping(value = "/list/{memberId}")
    public Result<List<MemberAddress>> list(@PathVariable Long memberId) {
        List<MemberAddress> addressList = memberReceiveAddressService.list(memberId);
        return Result.success(addressList);
    }

    @ApiOperation("获取收货地址详情")
    @GetMapping(value = "/info/{id}")
    public Result<MemberAddress> getItem(@PathVariable Long id) {
        MemberAddress address = memberReceiveAddressService.getItem(id);
        return Result.success(address);
    }
}
